package views.ope;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.MouseListener;
import java.sql.Date;
import java.text.ParseException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.text.MaskFormatter;

import models.cad.Aluno;
import models.cad.Desconto;
import models.cad.Turma;
import models.cad.Usuario;
import models.ope.Matricula;
import supers.GUIInternalFrame;
import supers.GUICadInterface;
import utils.GlobalDeclarations;
import views.SisDskGUI;

@SuppressWarnings("serial")
public class OpeMatGUI extends GUIInternalFrame implements GUICadInterface{
	public JFormattedTextField tfIs;
	public JButton btNovo;
	public JButton btExcluir;
	public JButton btListar;
	public JFormattedTextField tfCodDsc;
	public JFormattedTextField tfCodTur;
	public JFormattedTextField tfCodAlu;
	public JButton btNDesc;
	public JButton btNTur;
	public JButton btNAlu;
	public JFormattedTextField tfAno;
	public JLabel lbDsc;
	public JLabel lbTur;
	public JLabel lbAlu;
	public JLabel lbTur1;
	public JLabel lbTur2;
	public JLabel lbDsc1;
	
	public OpeMatGUI() {
		setTitle("Matr\u00EDculas");
		setSize(new Dimension(325, 320));
		try {
			
			JLabel lblAtiv = new JLabel("Aluno:");
			lblAtiv.setBounds(31, 94, 57, 14);
			getContentPane().add(lblAtiv);

			tfIs = new JFormattedTextField(new MaskFormatter("###"));
			tfIs.setText("");
			tfIs.setColumns(3);
			tfIs.setBounds(87, 31, 30, 20);
			getContentPane().add(tfIs);
			
			tfCodAlu = new JFormattedTextField(new MaskFormatter("###"));
			tfCodAlu.setColumns(3);
			tfCodAlu.setBounds(87, 91, 30, 20);
			getContentPane().add(tfCodAlu);
			
			lbAlu = new JLabel(".");
			lbAlu.setBounds(149, 94, 139, 14);
			getContentPane().add(lbAlu);
			
			JLabel lblAno = new JLabel("Ano:");
			lblAno.setBounds(31, 62, 34, 14);
			getContentPane().add(lblAno);
			
			tfAno = new JFormattedTextField(new MaskFormatter("####"));
			tfAno.setColumns(4);
			tfAno.setBounds(87, 59, 30, 20);
			getContentPane().add(tfAno);
			
			JLabel lblHorario = new JLabel("Turma:");
			lblHorario.setBounds(31, 125, 57, 14);
			getContentPane().add(lblHorario);
			
			tfCodTur = new JFormattedTextField(new MaskFormatter("###"));
			tfCodTur.setColumns(3);
			tfCodTur.setBounds(87, 122, 30, 20);
			getContentPane().add(tfCodTur);
			
			lbTur = new JLabel(".");
			lbTur.setBounds(149, 125, 139, 14);
			getContentPane().add(lbTur);
			
			JLabel lblDesconto = new JLabel("Desconto:");
			lblDesconto.setBounds(31, 206, 57, 14);
			getContentPane().add(lblDesconto);
			
			tfCodDsc = new JFormattedTextField(new MaskFormatter("###"));
			tfCodDsc.setColumns(3);
			tfCodDsc.setBounds(87, 203, 30, 20);
			getContentPane().add(tfCodDsc);
			
			lbDsc = new JLabel(".");
			lbDsc.setBounds(149, 206, 139, 14);
			getContentPane().add(lbDsc);
			
			btNAlu = new JButton("N");
			btNAlu.setBounds(119, 90, 23, 23);
			getContentPane().add(btNAlu);
			
			btNTur = new JButton("N");
			btNTur.setBounds(119, 121, 23, 23);
			getContentPane().add(btNTur);
			
			btNDesc = new JButton("N");
			btNDesc.setBounds(119, 202, 23, 23);
			getContentPane().add(btNDesc);
			
			JPanel panel_1 = new JPanel();
			panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			panel_1.setBounds(20, 254, 268, 26);
			getContentPane().add(panel_1);
			panel_1.setLayout(null);
			
			btNovo = new JButton("Novo");
			ImageIcon imageIcon = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtNovo));
			Image img = imageIcon.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btNovo.setIcon(new ImageIcon(img));
			btNovo.setBounds(1, 1, 80, 23);
			panel_1.add(btNovo);
			
			btExcluir = new JButton("Excluir");
			btExcluir.setEnabled(false);
			ImageIcon imageIcon2 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtExcluir));
			Image img2 = imageIcon2.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btExcluir.setIcon(new ImageIcon(img2));
			btExcluir.setBounds(91, 1, 85, 23);
			panel_1.add(btExcluir);
			
			btListar = new JButton("Listar");
			ImageIcon imageIcon3 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtListar));
			Image img3 = imageIcon3.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btListar.setIcon(new ImageIcon(img3));
			btListar.setBounds(186, 1, 80, 23);
			panel_1.add(btListar);
			
			lbTur1 = new JLabel(".");
			lbTur1.setBounds(149, 150, 139, 14);
			getContentPane().add(lbTur1);
			
			lbTur2 = new JLabel(".");
			lbTur2.setBounds(149, 175, 150, 14);
			getContentPane().add(lbTur2);
			
			lbDsc1 = new JLabel(".");
			lbDsc1.setBounds(149, 231, 139, 14);
			getContentPane().add(lbDsc1);
			
			JLabel label = new JLabel("Insc.:");
			label.setBounds(31, 34, 34, 14);
			getContentPane().add(label);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setListener(ActionListener ac, FocusListener fl, MouseListener ml) {
        tfIs.addFocusListener(fl);
        tfAno.addFocusListener(fl);
        tfCodAlu.addFocusListener(fl);
        tfCodTur.addFocusListener(fl);
        tfCodDsc.addFocusListener(fl);
        btNAlu.addActionListener(ac);
        btNTur.addActionListener(ac);
        btNDesc.addActionListener(ac);
        btNAlu.addFocusListener(fl);
        btNTur.addFocusListener(fl);
        btNDesc.addFocusListener(fl);
        btNovo.addActionListener(ac);
        btExcluir.addActionListener(ac);
        btListar.addActionListener(ac);
	}

	@Override
	public void limpar() {
		tfIs.setText("");
		tfAno.setText("");
		tfCodAlu.setText("");
		tfCodTur.setText("");
		tfCodDsc.setText("");
		lbAlu.setText("");
		lbDsc.setText("");
		lbDsc1.setText("");
		lbTur.setText("");
		lbTur1.setText("");
		lbTur2.setText("");
		btExcluir.setEnabled(false);
	}

	public void setDados(Object obj){
		Matricula mat = (Matricula) obj;
		if(mat.getIs() < 10)
			tfIs.setText("00" + String.valueOf(mat.getIs()));
		else if(mat.getIs() < 10)
				tfIs.setText("0" + String.valueOf(mat.getIs()));
			else
				tfIs.setText(String.valueOf(mat.getIs()));
		tfAno.setText(String.valueOf(mat.getAno()));
		tfCodAlu.setText(mat.getAluno().getCodigo());
		tfCodTur.setText(mat.getTurma().getCodigo());
		tfCodDsc.setText(mat.getDesconto().getCodigo());
	}
	
	public Matricula getDados(){
		Matricula mat = new Matricula();
		mat.setIs(Integer.valueOf(tfIs.getText()));
		mat.setAno(Integer.valueOf(tfAno.getText()));
		mat.setAluno(Aluno.getAluno(tfCodAlu.getText()));
		mat.setTurma(Turma.getTurma(tfCodTur.getText()));
		mat.setDesconto(Desconto.getDesconto(tfCodDsc.getText()));
		mat.setUsuario(Usuario.getUsuario(GlobalDeclarations.Is_Usuario));
		mat.setDtOpe(new Date(System.currentTimeMillis()));
		return mat;
	}

	@Override
	public JButton getBtNovo() {
		return btNovo;
	}

	@Override
	public JButton getBtExcluir() {
		return btExcluir;
	}

	@Override
	public JButton getBtListar() {
		return btListar;
	}
}
